//
//  QuDanNetworkManger.m
//  QuDanOEM
//
//  Created by mac on 2021/10/20.
#import "QuDanNetworkManger.h"
#import "QuDanResponseModel.h"
static id _instance;
static AFHTTPSessionManager *_session;
@interface QuDanNetworkManger()
@end
@implementation QuDanNetworkManger
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}
- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}
+ (instancetype)sharedNetworkManger
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _session = [AFHTTPSessionManager manager];
        [_session.requestSerializer setValue:@"application/x-www-form-urlencoded;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        _session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
        //_session.requestSerializer = [AFJSONRequestSerializer serializer];
        AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];
        response.removesKeysWithNullValues = YES;//去除空值
        _session.responseSerializer = response;//申明返回的结果是json
        _instance = [[self alloc] init];
    });
    return _instance;
}
-(void)GET:(NSString *)URLString parameters:(id)parameters success:(void (^)(QuDanResponseModel *response))success failure:(void (^)(NSError * error))failure
{
    [_session GET:URLString parameters:parameters headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        QuDanResponseModel *response = [QuDanResponseModel mj_objectWithKeyValues:responseObject];
        if (success) {
            success(response);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
-(void)POST:(NSString *)URLString parameters:(id)parameters success:(void (^)(QuDanResponseModel *response))success failure:(void (^)(NSError * error))failure
{
    [_session POST:URLString parameters:parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        QuDanResponseModel *response = [QuDanResponseModel mj_objectWithKeyValues:responseObject];
        if (success) {
            success(response);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
@end
